home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 11 / AMUG BBS in a Box Volume XI (April 1994) (MacWizards).iso / Files / Prog / D-G / Dynamic Math1.0.1.sit / Dynamic Math 1.0.1 / DM Demo ƒ / FormTest.p < prev   
Encoding:
Text File  |  1993-04-19  |  2.6 KB  |  131 lines  |  [TEXT/PJMM]

  1. program FormulaTest;
  2.  
  3. { test program to test the formular eval lib }
  4.  
  5.     uses
  6.  
  7.         DynamicMath;
  8.  
  9.     var
  10.         theFormula: Str255;
  11.         theText: CharsHandle;
  12.         theErr: integer;
  13.         theF: Formula;
  14.         es, ps: Str255; (* error strings *)
  15.         x: real;
  16.         y: real;
  17.         i: integer;
  18.         t: real;
  19.  
  20.         win: WindowPtr;
  21.         dlg: DialogPtr;
  22.         r: Rect;
  23.         xc, yc: integer;
  24.         w: integer;
  25.         dx: real;
  26.         TwoPeriod: Real;
  27.  
  28.         dummy: boolean;
  29.         theEvent: EventRecord;
  30.  
  31.     const
  32.         cGraphWindowID = 400;
  33.         cFormulaDialogID = 400;
  34.         cBadFormulaAlertID = 900;
  35.         cOKButton = 1;
  36.         cFormulaTextID = 2;
  37.  
  38.     function RealOut (theReal: real): str255;
  39.         var
  40.             s, s1: Str255;
  41.             n: integer;
  42.             s2: Str255;
  43.  
  44.     begin
  45.         if theReal < 0 then
  46.             s1 := '-'
  47.         else
  48.             s1 := '';
  49.         NumToString(TRUNC(theReal), s);
  50.         theReal := theReal - TRUNC(theReal);
  51.         n := ABS(TRUNC(theReal * 10000));
  52.         NumToString(n, s2);
  53.         s := Concat(s1, s, '.');
  54.         if n < 1000 then
  55.             s := Concat(s, '0');
  56.         if n < 100 then
  57.             s := Concat(s, '0');
  58.         if n < 10 then
  59.             s := Concat(s, '0');
  60.  
  61.         s := Concat(s, s2);
  62.         RealOut := s;
  63.     end;
  64.  
  65. (* GetFormula : Display Dialog until item = OK, then read the item string *)
  66.  
  67.     function GetFormula: Str255;
  68.         var
  69.             Dlg: DialogPtr;
  70.             itemHit: integer;
  71.             dummyType: integer;
  72.             theText: Handle;
  73.             dummyRect: Rect;
  74.             theFormula: Str255;
  75.  
  76.     begin
  77.         Dlg := GetNewDialog(cFormulaDialogID, nil, pointer(-1));
  78.         SetPort(Dlg);
  79.         repeat
  80.             ModalDialog(nil, itemHit);
  81.         until itemHit = cOKButton;
  82.         GetDItem(Dlg, cFormulaTextID, dummyType, theText, dummyRect);
  83.         GetIText(theText, theFormula);
  84.         DisposDialog(Dlg);
  85.         GetFormula := theFormula;
  86.     end;
  87.  
  88.  
  89. begin
  90.     InitCursor;
  91.     repeat
  92.         theFormula := GetFormula
  93.     until Length(theFormula) >= 1;
  94.     theText := CharsHandle(NewHandle(SizeOf(Chars)));
  95.     NEW(theF);
  96.     Str2Text(theFormula, theText);
  97.     theErr := Parse(theText, theF);
  98.     if theErr <> noErr then
  99.         begin
  100.             NumToString(theErr, es);
  101.             NumToString(gPos, ps);
  102.             ParamText(es, ps, '', '');
  103.             w := StopAlert(cBadFormulaAlertID, nil);
  104.             ExitToShell;
  105.         end;
  106.  
  107.     win := GetNEwWindow(cGraphWindowID, nil, pointer(-1));
  108.     SetPort(win);
  109.     TwoPeriod := 2 * Pi;
  110.     r := win^.portrect;
  111.     yc := ABS(r.bottom - r.top) div 2;
  112.     xc := ABS(r.right - r.left) div 2;
  113.     w := ABS(r.right - r.left);
  114.     dx := TwoPeriod / w;
  115.     x := -TwoPeriod / 2; (* from -1/2 T to + 1/2 T *)
  116.     moveto(0, yc);
  117.     LineTo(r.right, yc);
  118.     moveto(xc, 0);
  119.     LineTo(xc, r.bottom);
  120.     moveto(0, yc - TRUNC(theF.evaluate(x, y)));
  121.     for i := 1 to w do
  122.         begin
  123.             x := x + dx;
  124.             LineTo(i, yc - TRUNC(theF.evaluate(x, y)));
  125.         end;
  126.     repeat
  127.         dummy := GetNextEvent(everyEvent, theEvent);
  128.         SystemTask;
  129.     until (theEvent.what = keydown) or (theEvent.what = mouseDown);
  130.     DisposeWindow(win);
  131. end.